﻿#region copyright
//<copyright>
// Copyright(C) 2012 TrackerRealm Corporation
// This file is part of the open source project - Jazz. http://jazz.codeplex.com
// 
// Jazz is open software: you can redistribute it and/or modify it 
// under the terms of the GNU Affero General Public License (AGPL) as published by 
// the Free Software Foundation, version 3 of the License.
// 
// Jazz is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty 
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
//  See the GNU Affero General Public License (AGPL) for more details.
// 
// You should have received a copy of the GNU General Public 
// License along with Jazz.  If not, see <http://www.gnu.org/licenses/>.
//
// REMOVAL OF THIS NOTICE IS VIOLATION OF THE COPYRIGHT. 
//</copyright>
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using TrackerRealm.Jazz.Common;

namespace TrackerRealm.Jazz.Client
{
    /// <summary>
    /// Wraps the .net reflection class 'PropertyInfo'.
    /// </summary>
    public class jPropertyInfo: jMemberInfo
    {
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="member"></param>
        /// <param name="jObj"></param>
        internal jPropertyInfo(PropertyInfo member, jObject jObj)
            : base(member, jObj)
        {
            this.ReadAccessor = jReadAccessor.Empty;
            this.WriteAccessor = jWriteAccessor.Empty;
        }
        #region IsGrey
        /// <summary>
        /// Returns true if is not accessible due to a "Grant" attribute or the member is declared private.
        /// </summary>
        public override bool IsGrey
        {
            //get { return (!this.CanRead && !this.CanWrite); }
            get { return (this.ReadAccessor.IsGrey || this.WriteAccessor.IsGrey); }
        }
        #endregion
        #region PropertyInfo
        /// <summary>
        /// 
        /// </summary>
        public override PropertyInfo PropertyInfo
        {
            get { return (PropertyInfo)this.member; }
        }
        #endregion
        #region CanRead
        /// <summary>
        /// Returns true if 'IsProperty' is true and if the 'Read' accessor is usable
        /// </summary>
        public override bool CanRead
        {
            get
            {
                if (this.ReadAccessor.MethodInfo != null)  // will be null when empty
                {
                    if (this.jazzObject != null)
                    {
                        if (!this.jazzObject.IsAccessible) return false;
                        return  this.ReadAccessor.MethodInfo.IsGrantedAccess(this.jazzObject);
                    }
                    return this.PropertyInfo.CanRead;
                } 
                return false;
            }
        }
        #endregion
        #region CanWrite
        /// <summary>
        /// Returns true if 'IsProperty' is true and the 'Write' accessor is usable.
        /// </summary>
        public override bool CanWrite
        {
            get
            {
                if (this.WriteAccessor.MethodInfo != null)  // will be null when empty
                {
                    if (this.jazzObject != null)
                    {
                        if (!this.jazzObject.IsAccessible || this.jazzObject.IsReadOnly) return false;
                        return this.WriteAccessor.MethodInfo.IsGrantedAccess(this.jazzObject);
                    }
                    return this.PropertyInfo.CanWrite;
                }
                return false;
            }
        }
        #endregion
        #region Read
        /// <summary>
        /// If this property can be read ('CanRead')
        /// then the Read method returns the value of the property.
        /// </summary>
        /// <param name="jazzObj"></param>
        /// <returns></returns>
        public override object Read(jObject jazzObj)
        {
            return this.ReadAccessor.Get(jazzObj);
        }
        /// <summary>
        /// If this property can be read ('CanRead')
        /// then the Read method returns the value of the property.
        /// </summary>
        /// <returns></returns>
        public override object Read()
        {
            return this.ReadAccessor.Get(this.jazzObject);
        }
        #endregion
        #region Write
        /// <summary>
        /// 
        /// If this property can be written ('CanWrite')
        /// then the Write method set the value of the property.
        /// </summary>
        /// <param name="jazzObj"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public override object Write(jObject jazzObj, object value)
        {
            return this.WriteAccessor.Set(jazzObj, value);
        }
        /// <summary>
        /// 
        /// If this property can be written ('CanWrite')
        /// then the Write method set the value of the property.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public override object Write(object value)
        {
            return this.WriteAccessor.Set(this.jazzObject, value);
        }
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public override jReadAccessor ReadAccessor
        {
            get;
            internal protected set;
        }
        /// <summary>
        /// 
        /// </summary>
        public override jWriteAccessor WriteAccessor
        {
            get;
            internal protected set;
        }
        /// <summary>
        /// Returns true if this object is a Method.
        /// </summary>
        public override bool IsMethod
        {
            get { return false; }
        }
        /// <summary>
        /// Returns true if this object is a Property.
        /// </summary>
        public override bool IsProperty
        {
            get { return true; }
        }
    }
}
